package page;

import java.io.Console;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import entity4wms.*;
import dao.DAOdeliver;
import dao.DAOentry;
import dao.DAOgood;
import dao.DAOinventory;
import dao.DAOuaccount;

public final class printPage {
    public static String mainMenu(String[] menuStr) {
        int length = menuStr.length -1;
        System.out.println("****************************************************************");
        System.out.println("\t0. 返回上一级");
        for(int i = 1; i < menuStr.length; i++) {
            System.out.println("\t" + i + ". " +menuStr[i]);
        }
        System.out.println("****************************************************************");
        
        do{
            System.out.println("请输入选项0~" + length +"，按0返回");
            String choice = scannerInput();
            String regex = "[0-" + length +"]";
            if (choice.matches(regex))
                //System.out.println(choice);
                return choice;
                
            else{
                System.err.println("输入有误");
            }      
        }while(true);
            
    }

    public static String scannerInput(){
        Scanner sc = new Scanner(System.in);
        return sc.next();

    }

    /*
     * @param args：Uaccount增加
     */
    public static void addUaccount(){
        
        System.out.println("name: ");
        String name = scannerInput();
        System.out.println("password: ");
        String password = scannerInput();
        Uaccount oneUaccount = new Uaccount(name, password);
        DAOuaccount.addUaccount(oneUaccount);
        System.out.println("finished");
        
    }

    public  static void editUaccount() throws SQLException{
        DAOuaccount.queryUaccount();
        System.out.println("请输入id:");
        Integer id = Integer.parseInt(scannerInput());
        Uaccount account = new Uaccount();
        account.setUid(id);
        System.out.println("请输入name:");
        account.setUname(scannerInput());
        System.out.println("请输入password:");
        account.setUpassword(scannerInput());
        
        DAOuaccount.updateUaccount(account);
        System.out.println("finished");
        
    }

    public static void deleteUaccount() throws SQLException{
        DAOuaccount.queryUaccount();
        System.out.println("请输入id:");
        Integer id = Integer.parseInt(scannerInput());
        Uaccount account = new Uaccount();
        account.setUid(id);
        DAOuaccount.deleteUaccount(account);
        System.out.println("finished");
    }

    /*
     * 商品增删改
    */

    public static void addGood(){
        System.out.println("请输入品名：");
        String goodName=scannerInput();
        System.out.println("请输入规格：");
        String goodSize = scannerInput();
        System.out.println("请输入备注：");
        String goodRemark = scannerInput();
        Good aGood = new Good(goodName, goodSize, goodRemark);
        DAOgood.addGoods(aGood);
        System.out.println("finished");

    }

    public static void editGood(){
        System.out.println("请输入ID：");
        Integer id = Integer.parseInt(scannerInput());
        System.out.println("请输入修改品名：");
        String goodName = scannerInput();
        System.out.println("请输入修改规格：");
        String goodSize = scannerInput();
        System.out.println("请输入修改备注：");
        String goodRemark = scannerInput();
        Good agood = new Good(id,goodName, goodSize, goodRemark);
        DAOgood.updateGoods(agood);
        System.out.println("finished");


    }

    public static void deleteGood() throws SQLException{
        DAOgood.queryGoods();
        System.out.println("请输入id:");
        Integer id = Integer.parseInt(scannerInput());
        Good agood = new Good();
        agood.setGid(id);
        DAOgood.deleteGoods(agood);;
        System.out.println("finished");
    }
    
    public static Integer checkLogin() throws SQLException{
        Console cons = System.console();
        String username = null;
        char[] pwd = null;
        String passwd = null;
        int loginTimes = 3;
    
    do{
        username = cons.readLine("请输入用户名：");
        pwd = cons.readPassword("请输入密码：");
        passwd = new String(pwd);
        ArrayList<Uaccount>  accounts= DAOuaccount.checkUaccount(username);
        if (accounts == null || accounts.size()==0) {
        System.err.println("\t!!用户名输入有误!!\n");
        System.out.println("\n剩余登陆次数："+ --loginTimes);
        }else{
            Uaccount aUaccount = accounts.get(0);
            if (passwd.equals(aUaccount.getUpassword())) {
                System.out.println("登陆成功！！");
                Integer UID = aUaccount.getUid();
                return UID;
            }else{
                System.err.println("\t!!密码错误!!\n");
                System.out.println("\n剩余登陆次数："+ --loginTimes);
            }
        }

    }while(loginTimes > 0);
    System.out.println("退出系统");
    System.exit(1);
    return null;
    }

    /*
     * 入库
     */
    public static void addEntry(Integer userID) throws SQLException, ClassNotFoundException{
        DAOgood.queryGoods();
        System.out.println("请输入商品：");
        Integer gid = Integer.parseInt(scannerInput());
        System.out.println("请输入数量：");
        Integer num = Integer.parseInt(scannerInput());
        Entry entryNote = new Entry(gid, userID,num);
        entryNote = DAOentry.addEntry(entryNote);
        DAOinventory.addInventory(entryNote);

    }

    public static void delEntry() throws ClassNotFoundException, SQLException{
        System.out.println("请输入入库单号:");
        Integer eid = Integer.parseInt(scannerInput());
        Entry entryNote = new Entry();
        entryNote.setId(eid);
        DAOentry.removeEntry(entryNote);
        System.out.println("删除成功：");

    }
    public static void editEntry(Integer userID) throws ClassNotFoundException, SQLException{
        System.out.println("请输入入库单号：");
        Integer eid = Integer.parseInt(scannerInput());
        DAOgood.queryGoods();
        System.out.println("请输入商品编号：");
        Integer gid = Integer.parseInt(scannerInput());
        System.out.println("商品数量：");
        Integer num = Integer.parseInt(scannerInput());
        Entry entryNote = new Entry(eid,gid, userID, num);
        DAOentry.updateEntry(entryNote);
        System.out.println("完成更改");
    }
    public static void addDeliver(Integer userID) throws SQLException, ClassNotFoundException{
        DAOgood.queryGoods();
        System.out.println("请输入商品：");
        Integer gid = Integer.parseInt(scannerInput());
        System.out.println("请输入数量：");
        Integer num = Integer.parseInt(scannerInput());
        Deliver deliverNote = new Deliver(gid, userID,null,num);
        deliverNote=DAOdeliver.addDeliver(deliverNote);
        DAOinventory.addInventory(deliverNote);

    }

    public static void delDeliver() throws ClassNotFoundException, SQLException{
        System.out.println("请输入入库单号:");
        Integer id = Integer.parseInt(scannerInput());
        Deliver deliverNote = new Deliver();
        deliverNote.setId(id);;
        DAOdeliver.removeDeliver(deliverNote);
        System.out.println("删除成功：");

    }
    public static void editDeliver(Integer userID) throws ClassNotFoundException, SQLException{
        System.out.println("请输入入库单号：");
        Integer id = Integer.parseInt(scannerInput());
        DAOgood.queryGoods();
        System.out.println("请输入商品编号：");
        Integer gid = Integer.parseInt(scannerInput());
        System.out.println("商品数量：");
        Integer num = Integer.parseInt(scannerInput());
        Deliver deliverNote = new Deliver(id,gid, userID,null, num);
        DAOdeliver.updateDeliver(deliverNote);
        System.out.println("完成更改");
    }

    
}

